package com.huizhan.web.view;

import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import io.swagger.annotations.*;

@ApiModel(value =  "base64")
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
public class Base64String  implements Serializable {
	private static final long serialVersionUID =  271046682679543685L;

	/**
	 * base64
	 */
	@ApiModelProperty(value = "base64")
	private String base64;

	public String getBase64() {
		if(getContains()){
		  return getStrings()[1];
		}else{
		  return this.base64;
		}
	}

	public void setBase64(String base64) {
		this.base64 = base64;
	}
	
	private boolean  getContains(){		
	   return base64.contains(",");
	}
    
    private String [] strings;

	private String [] getStrings(){
		if(strings==null){
		   strings=base64.split(",");
	   }
	   return strings;
	}
    

    public String getFileName(){
		String path="";
		if(this.getContains()){     
			path= Util.md5HashCode(Util.base64ToInputStream(getStrings()[1]))+getFileSuffix();
		}else{
			path= Util.md5HashCode(Util.base64ToInputStream(base64))+getFileSuffix();
		}

		return path;
	}

    private	String getFileSuffix(){
		
		if(this.getContains()){                
			return"." + getStrings()[0].replace(";base64", "").split("/")[1];
		}else{
			return ".jpg";
		}
	}
}